package com.adtec.base.cloud.server.gateway.filter.factory;


import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.function.Predicate;

@Component
public class WeChatAuthRoutePredicateFactory
        extends AbstractRoutePredicateFactory<WeChatAuthRoutePredicateFactory.Config> {


    public WeChatAuthRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return exchange -> {
            String requestURI = exchange.getRequest().getURI().getPath();
            if (config.getFlag().equals("enterprisewechat")
                    && (
                    requestURI.startsWith("/wechatEnterprise")
                            || requestURI.startsWith("/wechatApp")
                            || requestURI.startsWith("/wechatSendmessage")
                            || requestURI.startsWith("/wechatUrl")
                            || requestURI.startsWith("/hospitalWeChat")
            )) {

                return true;  //表示匹配成功
            }
            return false;   //表示匹配失败
        };
    }

    //自定义参数args配置类
    public static class Config {
        private String flag; //该参数对应配置文件的args

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }
}